import requests
import base64
import urllib

params={
    "grant_type":"client_credentials",
    "client_id": "bt9brCIZy4MYnHzBUqpHOYbf",
    "client_secret": "SimOcnn7D9BDnvsh0euXmMuncCrZeXXZ"
}
res = requests.get("https://aip.baidubce.com/oauth/2.0/token",params=params)
token = res.json()['access_token']
# 定义头部信息
headers = {'Content-Type': 'application/x-www-from/urlencoded'}
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + token
# 读取图片
with open('test.png', 'rb')as f:
    tem_img = base64.b64encode(f.read())
# 进行base64编码
temp_data = {'image': tem_img}
# 请求视图接口
res = requests.post(url=url, data=temp_data, headers=headers)

mycode=''
word_dict = res.json()['words_result']
for key in word_dict:
    mycode+=key['words']
print(mycode)
